import { mock } from 'mockjs'

const users = [
  {
    id: 1,
    username: 'admin',
    password: '123456',
    realName: '管理员',
    avatar: '',
    roles: ['admin'],
    permissions: ['*']
  },
  {
    id: 2,
    username: 'user',
    password: '123456',
    realName: '普通用户',
    avatar: '',
    roles: ['user'],
    permissions: ['view']
  }
]

export const userApis = [
  // 登录接口
  {
    url: '/api/login',
    method: 'post',
    response: ({ body }) => {
      const user = users.find(
        (item) => item.username === body.username && item.password === body.password
      )
      if (user) {
        return {
          code: 200,
          message: '登录成功',
          data: {
            token: 'mock-token-' + Date.now(),
            userInfo: user
          }
        }
      } else {
        return {
          code: 401,
          message: '用户名或密码错误'
        }
      }
    }
  },
  // 获取用户信息
  {
    url: '/api/user/info',
    method: 'get',
    response: () => {
      return {
        code: 200,
        message: '获取成功',
        data: users[0]
      }
    }
  },
  // 登出接口
  {
    url: '/api/logout',
    method: 'post',
    response: () => {
      return {
        code: 200,
        message: '登出成功'
      }
    }
  }
]

export default users